package com.tang.thepath;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.github.chrisbanes.photoview.PhotoViewAttacher;
import com.tang.thepath.util.BitmapUtil;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class GalleryImagePreviewActivity extends BaseActivity {
    ImageView mImageView;
    PhotoViewAttacher mAttacher;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery_image_preview);

        hideActionBar();
        //View decorView = getWindow().getDecorView();
        // Hide both the navigation bar and the status bar.
        // SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
        // a general rule, you should design your app to hide the status bar whenever you
        // hide the navigation bar.
        //int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        //        | View.SYSTEM_UI_FLAG_FULLSCREEN;
        //decorView.setSystemUiVisibility(uiOptions);
        // Any implementation of ImageView can be used!
        mImageView = (ImageView) findViewById(R.id.iv_photo);
        // Attach a PhotoViewAttacher, which takes care of all of the zooming functionality.
        mAttacher = new PhotoViewAttacher(mImageView);

        final String imageUrl = getIntent().getStringExtra("url");

        // Set the Drawable displayed
        //Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
//        Bundle b= getIntent().getExtras();
//        byte[] bytes=b.getByteArray("bitmap");
//        Bitmap bmp= BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
//        mImageView.setImageBitmap(bmp);
//        mAttacher.update();

        showLoading("正在加载图片");
        Observable.create(new Observable.OnSubscribe<Bitmap>() {
            @Override
            public void call(Subscriber<? super Bitmap> subscriber) {
                Bitmap bitmap = BitmapUtil.getBitmapByUrl(imageUrl);
                subscriber.onNext(bitmap);
            }
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<Bitmap>() {
            @Override
            public void onCompleted() {
                dismisLoading();
            }

            @Override
            public void onError(Throwable e) {
                toast(e.getMessage());
            }

            @Override
            public void onNext(Bitmap bitmap) {
                dismisLoading();
                mImageView.setImageBitmap(bitmap);
                mAttacher.update();
            }
        });





    }
}
